package com.cx.filter;

import com.cx.pojo.Role;
import com.cx.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;

import javax.servlet.*;
import java.io.IOException;
import java.util.List;

@Configuration("roleFilter")
public class RoleFilter implements Filter {

    @Autowired
    private RoleService roleService;
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        System.out.println("=======初始化RoleService=========");
    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
//    查询所有角色
        List<Role> list = roleService.getAllRole();
//    将集合保存到request中
        servletRequest.setAttribute("list",list);


//    请求向下传递
        filterChain.doFilter(servletRequest,servletResponse);
    }

    @Override
    public void destroy() {

    }
}
